package com.gtl.meishi.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.gtl.meishi.util.DateBaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangke on 16/7/11.
 */
public class CollectionDao <T>{
    private Context context;
    private DateBaseHelper helper;
    private Dao<T, Integer> dao;
    private static final String TAG="TAG";
    public CollectionDao(Class<T> claz, Context context) {
        helper = DateBaseHelper.getDateBaseHelper(context);
        Log.i("TAG","helper-"+helper);
        try {
            dao = helper.getDao(claz);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param po 对象
     * @return 增加状态
     */
    public int add(T po) {

        try {
            return dao.create(po);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 查询全部
     */
    public List<T> queryAll(){
        try {
            return  dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Log.i(TAG,"获取全部数据");
        return new ArrayList<>();
    }
    /** 删除一条记录 */
    public int remove(T po) {
        try {
            return dao.delete(po);
        } catch (SQLException e) {
        }
        return -1;
    }
    /**
     * @param clumName
     * @param value
     * 更新
     */
    public void  update(String clumName,String value){
        try {
            dao.updateBuilder().updateColumnValue(clumName,value).where().eq("id","1");
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
    public int  update1(T po){
        try {
            return  dao.update(po);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;

    }

    /**
     * @param clumName
     * @param value
     * @return
     */
    public int   getByClum(String clumName,String value){
        try {
          return dao.queryBuilder().where().eq(clumName,value).query().size();
       //  return   dao.queryForEq(clumName,value).size();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }
}
